/*

Adder Extension - Time

*/
#include <AdderBytecodeEngine.h>
#include <AdderFunction.h>
#include <AdderObject.h>
#include <AdderBuiltinObject.h>
#include <AdderString.h>
#include <AdderLibraryMacros.h>
#include <windows.h>

namespace time
{
	void ReinitalizeBuiltins(AdderBytecodeEngine* engine);
	AdderValue * time_get(AdderBytecodeEngine* engine);
	
	class TimeModule: public AdderBuiltinObject<TimeModule>
	{
	public:
		TimeModule()
		{
			// We are not permitted to initalize the class in this way.
			assert(false);
		}

		TimeModule(AdderBytecodeEngine* engine)
		{
			this->engine = engine;
			this->InitalizeObject();
		}

		inline void InitalizeObject()
		{
			// This is where we register all of our variables
			// and functions.
			this->BuiltinRegisterFunction("current", &TimeModule::current);
		}

		AdderValue * current(AdderBytecodeEngine* engine)
		{
			SYSTEMTIME st;
			GetSystemTime (&st);
			
			AdderValue * a = new AdderValue();
			a->type = AdderValueType::NUMBER;
			a->r_number = st.wMilliseconds + st.wSecond * 1000 + st.wMinute * 1000 * 60 + st.wHour + 1000 * 60 * 60;

			return a;
		}
	};

	void InitalizeBuiltins(AdderBytecodeEngine* engine)
	{
		// Here we initalize the engine and provide it links to our own
		// builtins that we have defined.
		engine->BuiltinRegisterFunction("time_get", &time_get);

		/*engine->BuiltinRegisterScopeChange("Time", &ReinitalizeBuiltins);
		AdderValue * o = new AdderValue();
		o->type = AdderValueType::OBJECT;
		o->r_object = new TimeModule(engine);
		engine->BuiltinRegisterVariable("time", o);*/
	}

	void ReinitalizeBuiltins(AdderBytecodeEngine* engine)
	{
		// This function is called when we need to reload the builtin variables
		// because of a scope change.
		//
		// NOTE: You only need to reload the variables here, not classes or
		//       functions.
		
		/*AdderValue * o = new AdderValue();
		o->type = AdderValueType::OBJECT;
		o->r_object = new TimeModule(engine);
		engine->BuiltinRegisterVariable("time", o);*/
	}

	AdderValue * time_get(AdderBytecodeEngine* engine)
	{
		SYSTEMTIME st;
		GetSystemTime (&st);
		
		AdderValue * a = new AdderValue();
		a->type = AdderValueType::NUMBER;
		a->r_number = st.wMilliseconds + st.wSecond * 1000 + st.wMinute * 1000 * 60 + st.wHour + 1000 * 60 * 60;

		return a;
	}
}